package tests;

import tester.*;

/**
 * Tests enumerations using a simple calculator
 * @author Weston Jossey
 * @since Feb 1. 2009
 */
@Example
public class EnumTest {

	private EnumTest() {
	}

	@SuppressWarnings("unused")
	@TestMethod
	private void testOperators(Tester t) {
		MyMath math = new MyMath();

		/* Test the different operators*/
		t.checkExpect(math.compute(Operator.ADD, 5, 5), 10);
		t.checkExpect(math.compute(Operator.SUBTRACT, 5, 5), 0);
		t.checkExpect(math.compute(Operator.MULTIPLY, 5, 5), 25);
		t.checkExpect(math.compute(Operator.DIVIDE, 5, 5), 1);
		
		/*Check for proper exception handling*/
		try {
			t.checkExpect(math.compute(Operator.DIVIDE, 5, 0), -1);
			t.fail();
		} catch (RuntimeException exception) {
			t.success("Division by zero test passed");
		}

	}

	/* Run the full report */
	public static void main(String[] args) {
		Tester.runFullReport(new EnumTest());
	}

}

/**
 * Operator enumerated type
 * @author Weston Jossey
 *
 */
enum Operator {
	ADD, SUBTRACT, DIVIDE, MULTIPLY;
}

/**
 * Simple math calculator that utilizes
 * enumerated types
 * @author Weston Jossey
 *
 */
class MyMath {

	public MyMath() {
	}

	/**
	 * Computes the result of a math operation with the given values.
	 * 
	 * @param op
	 *            operator to use
	 * @param val1
	 *            value 1
	 * @param val2
	 *            value 2
	 * @return the resulting value of the computation
	 */
	public int compute(Operator op, int val1, int val2) {
		if (op == Operator.ADD) {
			return val1 + val2;
		} else if (op == Operator.SUBTRACT) {
			return val1 - val2;
		} else if (op == Operator.DIVIDE) {
			if (val2 != 0) {
				return val1 / val2;
			} else {
				throw new ArithmeticException();
			}
		} else if (op == Operator.MULTIPLY) {
			return val1 * val2;
		} else {
			throw new RuntimeException("Operator not found");
		}
	}

}